package com.sany.struts.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sany.struts.form.RetailerCreateForm;
import com.sany.util.DateConverter;
import com.sany.util.SapUtil;
import com.sany.web.beans.TireBrandBean;
import com.sany.web.beans.TireRecordBean;
import com.sany.web.beans.TrainBean;
import com.sany.web.database.DBManager;
import com.sany.web.database.SapDatabase;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Client;
import com.sap.mw.jco.JCO.Function;
import com.sap.mw.jco.JCO.ParameterList;

public class RedetailerUpdateAction extends DispatchAction implements
		Serializable {
	private static final long serialVersionUID = 1L;
	public ActionForward updateRedetailer(ActionMapping mapping, ActionForm forms,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RetailerCreateForm retailerUpdateForm=(RetailerCreateForm)forms;
		DateConverter dateConverter=new DateConverter();
		SapDatabase factory = SapUtil.getSapDatabase();
		Client conn = null;
		IRepository iRepository = factory.getConnection();
		IFunctionTemplate ftemplate = iRepository
				.getFunctionTemplate("ZVRI_I_SRP_BP_CHANGE_ALL");
		Function function = new Function(ftemplate);
		conn = JCO.getClient(DBManager.SID);
		ParameterList inparm = function.getImportParameterList();
		//I_LOGON
		JCO.Structure iLogon= inparm.getStructure("I_LOGON");
		com.sany.web.session.WebSession ws=(com.sany.web.session.WebSession) request.getSession().getAttribute("user");
		iLogon.setValue(ws.getSaleOrg(), "VKORG");
		iLogon.setValue(ws.getUserName(), "USERID");
		iLogon.setValue(ws.getBpNo(), "PARTNER");
		iLogon.setValue(ws.getLanguage(), "LANGU");
		
		//I_SIGN
		inparm.setValue("RETAILER", "I_SIGN");
		//I_HEAD
		inparm.setValue(ws.getBpNo(), "I_HEAD");
		//I_BP_CUSTOMIZE_HEAD
		JCO.Structure iHead= inparm.getStructure("I_BP_CUSTOMIZE_HEAD");
		iHead.setValue(retailerUpdateForm.getComType(), "STORE_S");
		iHead.setValue(new Integer(retailerUpdateForm.getComNum()).intValue(), "STAFF");
		if(retailerUpdateForm.getSalNum()!=null && !retailerUpdateForm.getSalNum().equals("")){
			iHead.setValue(new Integer(retailerUpdateForm.getSalNum()).intValue(), "STAFF_SALES");//salNum
		}else{
			iHead.setValue(0, "STAFF_SALES");//salNum
		}
		if(retailerUpdateForm.getTechNum()!=null && !retailerUpdateForm.getTechNum().equals("")){
			iHead.setValue(new Integer(retailerUpdateForm.getTechNum()).intValue(), "MECHANICS");//techNum
		}else{
			iHead.setValue(0, "MECHANICS");//techNum
		}
		if(retailerUpdateForm.getSaleHistory()!=null && !retailerUpdateForm.getSaleHistory().equals("")){
			iHead.setValue(new Integer(retailerUpdateForm.getSaleHistory()).intValue(), "TIRE_BU_HIS");
		}else{
			iHead.setValue(0, "TIRE_BU_HIS");
		}
		if(retailerUpdateForm.getTypeSum()!=null && !retailerUpdateForm.getTypeSum().equals("")){
			iHead.setValue(new Integer(retailerUpdateForm.getTypeSum()).intValue(), "TIRE_SOB");//typeSum
		}else{
			iHead.setValue(0, "TIRE_SOB");//typeSum
		}
		iHead.setValue("", "VISIT_PER");
		iHead.setValue(retailerUpdateForm.getRetailerLoop(), "VISIT_LOOP");
		iHead.setValue(retailerUpdateForm.getBrandId(), "DZ_GOODYEAR");
		iHead.setValue(retailerUpdateForm.getSize(), "DZ_SIZE");
		iHead.setValue(retailerUpdateForm.getInstalldate(), "DZ_INS_DATE");
		iHead.setValue(retailerUpdateForm.getTire(), "SI_TIRE");
		iHead.setValue("", "SI_MAINTAINENCE");
		iHead.setValue(retailerUpdateForm.getCosmetic(), "SI_BEAUTY");
		iHead.setValue(retailerUpdateForm.getAutomoRepair(), "SI_REPAIR");
		iHead.setValue(retailerUpdateForm.getProfit(), "SI_PERCENTAGE");
		iHead.setValue(retailerUpdateForm.getArea(), "SF_AREA");
		iHead.setValue(retailerUpdateForm.getStation(), "SF_WORKING_BAY");
		iHead.setValue(retailerUpdateForm.getWashRoom(), "SF_LAUNDRY");
		iHead.setValue(retailerUpdateForm.getRestRoom(), "SF_REST_ROOM");
		iHead.setValue(retailerUpdateForm.getPositionFinder(), "SF_ALIGNMENT");
		iHead.setValue(retailerUpdateForm.getTireRepair(), "SF_TIRE_REPAIR");
		iHead.setValue(retailerUpdateForm.getTireMachine(), "SF_FITTING");
		iHead.setValue(retailerUpdateForm.getBalenceMachine(), "SF_BALANCING");
		iHead.setValue(retailerUpdateForm.getServiceCar(), "SF_SERVICE_VAN");
		iHead.setValue(retailerUpdateForm.getServiceContract(), "SF_SC");
		iHead.setValue(retailerUpdateForm.getCarWash(), "SF_CAR_WASH");
		
		//IT_ITEM01
		JCO.Table item01= inparm.getTable("IT_ITEM01");
		item01.appendRow();
		item01.setValue("", "PARTNER");
		if(retailerUpdateForm.getContractPeriod()!=null && !retailerUpdateForm.getContractPeriod().equals("")){
			item01.setValue(dateConverter.toSapDate(retailerUpdateForm.getContractPeriod()), "VALIDITY_DATE");//��ͬ����
		}
		item01.setValue(retailerUpdateForm.getContractAmount(), "AMOUNT");
		item01.setValue("", "CURRENCY");
		item01.setValue(retailerUpdateForm.getInvestType(), "MODE_I");
		item01.setValue(retailerUpdateForm.getInvestRecord(), "RECORD");
		
		
		//IT_ITEM02
		JCO.Table item02= inparm.getTable("IT_ITEM02");
		List tireBrandList=retailerUpdateForm.getTireBrandList();
		TireBrandBean tireBrandBean=null;
		for(int i=0;i<tireBrandList.size();i++){
			item02.appendRow();
			tireBrandBean = (TireBrandBean)tireBrandList.get(i);
			if(tireBrandBean.getTireBrand()==null){
				break;
			}
			item02.setValue("", "PARTNER");
			item02.setValue(tireBrandBean.getTireBrand(), "BRAND");
			item02.setValue(tireBrandBean.getChannel(), "SUPPLIERS");
			item02.setValue(tireBrandBean.getPay(), "PAYMENT_TERMS");
			item02.setValue(tireBrandBean.getPolicy(), "KEY_COM_POLICY");
		}
		
		//IT_ITEM03
		JCO.Table item03= inparm.getTable("IT_ITEM03");
		TireRecordBean tireRecordBean=null;
		List tireRecordList=retailerUpdateForm.getTireRecordList();
		for(int i=0;i<tireRecordList.size();i++){
			item03.appendRow();
			tireRecordBean = (TireRecordBean)tireRecordList.get(i);
			if(tireRecordBean.getBrand()==null){
				break;
			}
			item03.setValue("", "PARTNER");
			item03.setValue(tireRecordBean.getYear(), "YEAR1");
			item03.setValue(tireRecordBean.getBrand(), "BRAND");
			item03.setValue(tireRecordBean.getAimActual(), "AIM_ACTUAL");
			item03.setValue(tireRecordBean.getSize14(), "BELOW14");
			item03.setValue(tireRecordBean.getSize16(), "EQ15OR16");
			item03.setValue(tireRecordBean.getSize17(), "EQ17");
			item03.setValue(tireRecordBean.getSize18(), "ABOVE18");
		}
		
		//IT_ITEM04
		JCO.Table item04= inparm.getTable("IT_ITEM04");
		item04.appendRow();
		item04.setValue("", "PARTNER");
		item04.setValue("", "YEAR1");
		item04.setValue("", "BRAND");
		item04.setValue(retailerUpdateForm.getJan(), "JAN");
		item04.setValue(retailerUpdateForm.getFeb(), "FEB");
		item04.setValue(retailerUpdateForm.getMar(), "MAR");
		item04.setValue(retailerUpdateForm.getApr(), "APR");
		item04.setValue(retailerUpdateForm.getMay(), "MAY");
		item04.setValue(retailerUpdateForm.getJun(), "JUN");
		item04.setValue(retailerUpdateForm.getJul(), "JUL");
		item04.setValue(retailerUpdateForm.getAug(), "AUG");
		item04.setValue(retailerUpdateForm.getSep(), "SEP");
		item04.setValue(retailerUpdateForm.getOct(), "OCT");
		item04.setValue(retailerUpdateForm.getNov(), "NOV");
		item04.setValue(retailerUpdateForm.getDec(), "DECM");
		
		//IT_ITEM05
		JCO.Table item05= inparm.getTable("IT_ITEM05");
		List   trainList=retailerUpdateForm.getTrainList();
		TrainBean trainBean=null;
		for(int i=0;i<trainList.size();i++){
			trainBean=(TrainBean)trainList.get(i);
			item05.appendRow();
			if(trainBean.getTrainTeacher()==null){
				break;
			}
			item05.setValue("", "PARTNER");
			//dateConverter.toSapDate(trainBean.getTrainDate())
			item05.setValue("", "T_DATE");
			item05.setValue(trainBean.getTrainContent(), "COURSE");
			item05.setValue(trainBean.getTrainAddress(), "LOCATION");
			item05.setValue(trainBean.getTrainTeacher(), "TRAINER");
			item05.setValue(trainBean.getTrainPerson(), "TRAINEE");
			item05.setValue(trainBean.getTrainOnJob(), "DUTY");
			item05.setValue(trainBean.getTrainComment(), "COMMENTS");
		}
		
		conn.execute(function);
		return mapping.findForward("retailerList");
	}
}
